<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Good;
use app\models\GoodCate;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class GoodController extends BaseController
{
    //商品类别列表
    public function actionGoodCateList(){
      $name = Yii::$app->request->post('name', '');
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 20);
      $query = GoodCate::find()->where(['status' => [0,1]]);
      if ($name) {
        $query->andWhere(['like', 'name', $name]);
      }
      $cate_list = $query->orderBy('sort desc, created_at desc')->limit($page_size)->offset($page_size*$page)->all();
      $cate_data = [];
      if ($cate_list) {
        foreach ($cate_list as $v) {
          $cate_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'logo' => $v->logo,
            'sort' => $v->sort,
            'status' => $v->status,
            'status_name' => $v->status == 1 ? '启用' : '禁用',
            'created_time' => date("Y-m-d H:i", $v->created_at)
          ];
        }
      }
      return Re::data([
        'data' => $cate_data,
        'totalPage' => $query->count(),
        'currentPage' => $page,
        'perPage' => $page_size
      ]);
    }

    //编辑商品类别
    public function actionEditGoodCate(){
      $id = Yii::$app->request->post('id', 0);
      $post = Yii::$app->request->post();
      $cate_old = GoodCate::find()->where(['name' => $post['name'], 'status' => [0,1]])->andWhere(['<>', 'id', $id])->one();
      if ($cate_old) {
        return Re::data(null, '类别信息已存在', Re::ERROR);
      }
      return $this->edit(GoodCate::class, $id, $post);
    }

    //修改商品类别状态
    public function actionChangeGoodCateStatus(){
      $id = Yii::$app->request->post('id', 0);
      $status = Yii::$app->request->post('status', 0);
      $good_cate = GoodCate::findOne($id);
      if (!$good_cate) {
        return Re::data(null, '商品类别信息读取失败', Re::ERROR);
      }
      $good_cate->status = $status;
      $good_cate->save();
      return Re::data();
    }

    //商品类别筛选项数据源
    public function actionGoodCateForSelect(){
      $list = GoodCate::find()->where(['status' => 1])->select('id, name')->orderBy('sort desc')->all();
      return Re::data(['data' => $list]);
    }

    //商品列表
    public function actionGoodList(){
      $name = Yii::$app->request->post('name', '');
      $cate_id = Yii::$app->request->post('cate_id', 0);
      $status = Yii::$app->request->post('status', 0);
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 20);
      $created_range = Yii::$app->request->post('created_range', '');
      $query = Good::find()->where(['status' => [0,1]]);
      if ($name) {
        $query->andWhere(['like', 'name', $name]);
      }
      if ($cate_id) {
        $query->andWhere(['cate_id' => $cate_id]);
      }
      if ($status > -1) {
        $query->andWhere(['status' => $status]);
      }
      if ($created_range) {
        $created_range_arr = explode('-', $created_range);
        $start_at = $created_range_arr[0];
        $end_at = $created_range_arr[1] + 86400;
        $query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
      }
      $good_list = $query->orderBy('sort desc')
        ->limit($page_size)
        ->offset($page_size*$page)
        ->all();
      $good_data = [];
      if ($good_list) {
        foreach ($good_list as $v) {
          $good_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'cate_id' => $v->cate_id,
            'cate_name' => $v->cate->name,
            'img' => $v->img,
            'imgs' => $v->imgs,
            'sale_count' => $v->sale_count,
            'store' => $v->store,
            'price' => $v->price,
            'price_dis' => $v->price_dis,
            'desc' => $v->desc,
            'sort' => $v->sort,
            'status' => $v->status,
            'status_name' => $v->status == 1 ? '启用' : '禁用',
            'created_time' => date("Y-m-d H:i", $v->created_at)
          ];
        }
      }
      return Re::data([
        'data' => $good_data,
        'totalPage' => $query->count(),
        'currentPage' => $page,
        'perPage' => $page_size
      ]);
    }

    //编辑商品
    public function actionEditGood(){
      $id = Yii::$app->request->post('id', 0);
      $post = Yii::$app->request->post();
      return $this->edit(Good::class, $id, $post);
    }

    //修改商品状态
    public function actionChangeGoodStatus(){
      $id = Yii::$app->request->post('id', 0);
      $status = Yii::$app->request->post('status', 1);
      $good = Good::findOne($id);
      if (empty($good)) {
        return Re::data(null, '商品信息读取失败', Re::ERROR);
      }
      $good->status = $status;
      if(!$good->save()){
        return Re::data(null, $good->showErrors(), Re::ERROR);
      }
      return Re::data();
    }

    //商品详情
    public function actionGoodDetail(){
      $id = Yii::$app->request->post('id', 0);
      $good = Good::findOne($id);
      if (!$good) {
        return Re::data(null, '商品信息读取失败', Re::ERROR);
      }
      $data = [
        'id' => $good->id,
        'name' => $good->name,
        'cate_id' => $good->cate_id,
        'img' => $good->img,
        'imgs' => $good->imgs,
        'sale_count' => $good->sale_count,
        'store' => $good->store,
        'price' => $good->price,
        'price_dis' => $good->price_dis,
        'desc' => $good->desc,
        'detail_info' => $good->detail_info,
        'sort' => $good->sort,
        'status' => $good->status,
        'created_time' => date("Y-m-d H:i", $good->created_at)
      ];
      return Re::data(['data' => $data]);
    }

    //商品列表筛选项数据源
    public function actionGoodListForSelect(){
      $good_list = Good::find()->where(['status' => 1])->orderBy('created_at desc')->all();
      $good_data = [];
      if ($good_list) {
        foreach ($good_list as $v) {
          $good_data[] = [
            'id' => $v->id,
            'name' => mb_strlen($v->name) > 30 ? mb_substr($v->name, 0, 30).'...' : $v->name,
          ];
        }
      }
      return Re::data(['data' => $good_data]);
    }
}
